package com.chat.api.test;

import com.jacob.com.ComThread;
import com.jacob.com.Variant;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import lombok.SneakyThrows;
import java.awt.*;
import java.awt.event.InputEvent;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


public class HappyOnHook {

    private static int x = 0;
    private static int y = 0;

    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 50);
        calendar.set(Calendar.SECOND, 0);

        Date time = calendar.getTime();

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @SneakyThrows
            public void run() {
                Runtime.getRuntime().exec("D:\\app\\qywx\\WXWork\\WXWork.exe");
                start();
            }
        }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    }

    public static void start(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                change();
            }
        }, 1000,10000);
    }

    // 鼠标移动
    public static void change() {
        Robot robot;
        try {
            robot = new Robot();
//            robot.mouseMove(x,y);
            robot.mouseMove(x, y);
            //防止鼠标移动到屏幕死角
            x = x + 200;
            y = y + 100;
            if (x >= 2000) {
                x = 0;
            }
            if (y >= 1000) {
                y = 0;
            }
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }


    /**
     * word转成为PDF
     * @param fileName
     * @param toFileName
     * @return
     */
    public static int word2Pdf(String fileName,String toFileName){
        System.out.println("启动word...");
        Long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("word.Application");
            app.setProperty("Visible", new Variant(false));
            //打开word文件
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[]{
                    fileName, new Variant(false),new Variant(true) },new int[1]).toDispatch();
            System.out.println("打开文档..."+ fileName);
            System.out.println("转换文档到PDF..."+ toFileName);
            File tofile = new File(toFileName);
            if (tofile.exists()){
                tofile.delete();
            }
            Dispatch.invoke(doc,"SaveAs", Dispatch.Method,new Object[]{
                    toFileName,new Variant(17)},new int[1]);
            Long end = System.currentTimeMillis();
            System.out.println("转换完成...用时：" + (end - start) + "ms.");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("=========Error:文档转换失败：" + e.getMessage());
        }catch (Throwable t){
            t.printStackTrace();
        }finally {
            //关闭word
            Dispatch.call(doc,"Close",false);
            System.out.println("关闭文档");
            if (app != null){
                app.invoke("Quit",new Variant[]{});
            }
            //如果没有这句话winword.exe进程将不会关闭
            ComThread.Release();
            return 1;
        }
    }

}

